/*
============================================================================
 Name        : TCPSocketEngine.h
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CTCPSocketEngine declaration
============================================================================
*/

#ifndef TCPSOCKETENGINE_H
#define TCPSOCKETENGINE_H

// For CActive, link against: euser.lib
// For RTimer, link against: euser.lib
#include <e32base.h>
#include <es_sock.h>
#include <in_sock.h>

#include "TCPSocketTimer.h"
#include "TCPSocketSender.h"
#include "TCPSocketReceiver.h"
#include "TCPSocketSSLRev.h"
#include "TCPSocketSSL.h"

class CTCPSocketEngine : public CActive,
						 public CTCPSocketSender::MObserver, 
						 public CTCPSocketReceiver::MObserver, 
						 public CTCPSocketTimer::MTimerObserver,
						 public CTCPSocketSSL::MObserver

{

public: // Observer interface
	class MTcpObserver
	{
	public:
		virtual void OnConnectL(TInt aError) = 0;
		virtual void OnSendL(const TDesC8& aMsg, TInt aError) = 0;
		virtual void OnReceiveL(const TDesC8& aMsg, TInt aError) = 0;
		virtual void GetIap(TUint32& aIap)=0;
		virtual void SSLHandShakeComplete(TInt err)=0;
	};

public: // Enumeration
	enum TState
	{
		EStateIdle,
		EStateDomainResolved,
		EStateCreateIapConnection,
		EStateConnecting,
		EStateConnected
	};

public:
	static CTCPSocketEngine* NewL(MTcpObserver& aObserver);
	
	// Cancel and destroy
	~CTCPSocketEngine();
private:
	/**
	* EPOC default constructor.
	*/
	CTCPSocketEngine(MTcpObserver& aObserver);
	void ConstructL();

public: // New functions
	// Function for making the initial request
	void Start(TTimeIntervalMicroSeconds32 aDelay);
	
	void StartSSL();
	
	void StopSSL();



private: // From CActive
	// Handle completion
	void RunL();
	
	// How to cancel me
	void DoCancel();
	
	void CreateIApConnection();
	
	void ResolveDomain();
	// Override to handle leaves from RunL(). Default implementation causes
	// the active scheduler to panic.
	//void RunError(TInt aError);


public: // New functions
	TState State();
	void ConnectL(const TDesC& aAddr, TUint aPort);
	void DoConnect();
	void Disconnect();

	void SendL(const TDesC8& aMsg);
	void ReceiveL();

protected: // Functions from CTcpSocketTimer::MTimerObserver
	void OnTimerL(TInt aError);

protected: // Functions from CTcpSocketSender::MObserver
	void OnSendL(const TDesC8& aMsg, TInt aError);

protected: // Functions from CTcpSocketReceiver::MObserver
	void OnReceiveL(const TDesC8& aData, TInt aError);

	void OnSendSSL(const TDesC8& aMsg, TInt aError) ;
	void OnHandComplete(TInt aError);
	void OnRevSSL(const TDesC8& aData, TInt aError);
	

	
	
	
private: // member variables
	MTcpObserver& iObserver;
	RSocketServ iSocketServ;
	RConnection iConnection;
	RSocket iListenSocket;
	RSocket iSocket;
	TBuf<40>            iServer;
	TUint16             iPort;
	TInetAddr iAddr;
	CTCPSocketTimer* iTimer;
	CTCPSocketSender* iSender;
	CTCPSocketReceiver* iReceiver;
	RHostResolver       iResolver;
	TNameEntry          iNameEntry;
	TUint32 iIAP;
	TState iState;
	TInt   iConntectRetry;
	
	TBool   iSocketStatus;
	TBool   iHandShakeComplete;
	CTCPSocketSSL*     iSSLSocket;
	
	HBufC8*            iTmpMsg;
	
};

#endif
